﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OrderingSystem.Constants;
using EShop.DataAccess;
using EShop.BussinessObjects;
using EShop.Common;
using EShop.BusinessLogic;

namespace OrderingSystem
{
    public partial class MainForm : Form
    {
        public event EventHandler IncomingCallStarted;
        public event EventHandler IncomingCallEnded;
        public int testCounter = 0;
        public string[] testNumbers = new string[5] { "+4321345678", "+10384798798", "+9874568723", "+2354234534", "+7865473400" };

        public MainForm()
        {
            InitializeComponent();
            ShowData_in_grdCustomers();
            tabControl1.SelectedIndex = 1;
            timer1.Start();
            timer2.Stop();
        }

        private void ShowData_in_grdCustomers()
        {
            lblStatus.Text = string.Empty;
            User user = new User();
            user.ID = (int)cnstDatabase.DBOperationResult.ValueIsNotEffective;
            List<User> userList = CBLUser.SelectCustomer(user, txtSearchCustomer.Text.Trim());

            DataTable dt = new DataTable();
            dt.Columns.Add("RowNo");
            dt.Columns.Add("UserID");
            dt.Columns.Add("FirstName");
            dt.Columns.Add("LastName");

            for (int i = 0; i < userList.Count; i++)
            {
                dt.Rows.Add();
                dt.Rows[i]["RowNo"] = i + 1;
                dt.Rows[i]["UserID"] = userList[i].ID;
                dt.Rows[i]["FirstName"] = userList[i].Contact.FirstName;
                dt.Rows[i]["LastName"] = userList[i].Contact.LastName;

            }
            grdCustomer.DataSource = dt;
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
            Point point = new Point(Width - cnstForm.AdvertiseWidth - 15, 25);
            adsRight.Location = point;

            Size size = new Size(Width - 263, pnlIncomingCall.Size.Height);
            pnlIncomingCall.Size = size;

            size = new Size(Width - 263, Height - 202);
            tabControl1.Size = size;

            size = new Size(Width - 280, Height - 310);
            grdCustomer.Size = size;

            point = new Point(Width - 530, txtSearchOrder.Location.Y);
            txtSearchOrder.Location = point;
            point = new Point(Width - 380, btnSearchOrder.Location.Y);
            btnSearchOrder.Location = point;

            point = new Point(Width - 530, txtSearchCustomer.Location.Y);
            txtSearchCustomer.Location = point;
            point = new Point(Width - 380, btnSearchCustomer.Location.Y);
            btnSearchCustomer.Location = point;

            point = new Point(Width - 530, txtSearchProduct.Location.Y);
            txtSearchProduct.Location = point;
            point = new Point(Width - 380, btnSearchProduct.Location.Y);
            btnSearchProduct.Location = point;

        }

        private void grdCustomer_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (e.ColumnIndex == grdCustomer.Columns["EditCustomer"].Index && e.RowIndex >= 0)
                {
                    DataGridView gridView = (DataGridView)sender;
                    ContactEdit customerEdit = new ContactEdit();
                    User _user = new User();
                    _user.ID = Convert.ToInt32(gridView["UserID", e.RowIndex].Value);
                    List<User> _userList = CBLUser.SelectCustomer(_user, string.Empty);
                    customerEdit.CurrentUser = _userList[0];
                    customerEdit.IsNew = false;
                    customerEdit.UserTypeID = cnstUserType.Customer;
                    customerEdit.btnSaveClicked += new EventHandler(customerEdit_btnSaveClicked);
                    customerEdit.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void btnAddNewCustomer_Click(object sender, EventArgs e)
        {
            ContactEdit customerEdit = new ContactEdit();
            customerEdit.IsNew = true;
            customerEdit.UserTypeID = cnstUserType.Customer;
            customerEdit.btnSaveClicked += new EventHandler(customerEdit_btnSaveClicked);
            customerEdit.ShowDialog(this);
        }

        void customerEdit_btnSaveClicked(object sender, EventArgs e)
        {
            ShowData_in_grdCustomers();
            lblStatus.Text = "Changes have been saved successfully.";
        }

        private void btnSearchCustomer_Click(object sender, EventArgs e)
        {

            ShowData_in_grdCustomers();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            IncomingCallStarted += new EventHandler(MainForm_IncomingCallStarted);
            lblNumberCall.Text = testNumbers[testCounter];
            if (testCounter < 4) testCounter++;
            timer1.Stop();
            timer2.Start();
            if (IncomingCallStarted != null)
                IncomingCallStarted(sender, e);
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            IncomingCallEnded += new EventHandler(MainForm_IncomingCallEnded);
            timer2.Stop();
            if (IncomingCallEnded != null)
                IncomingCallEnded(sender, e);
        }

        void MainForm_IncomingCallStarted(object sender, EventArgs e)
        {
            pnlReady.Visible = false;
            Contact _contact = CDAContact.SelectByNumber(lblNumberCall.Text.Trim());
            if (_contact.ID > 0)
            {
                lblContactNameCall.Text = _contact.FirstName + " " + _contact.LastName;
            }
        }

        void MainForm_IncomingCallEnded(object sender, EventArgs e)
        {
            pnlReady.Visible = true;
            timer1.Start();
        }

        private void btnCancelCall_Click(object sender, EventArgs e)
        {
            IncomingCallEnded += new EventHandler(MainForm_IncomingCallEnded);
            timer2.Stop();
            if (IncomingCallEnded != null)
                IncomingCallEnded(sender, e);
        }

        private void btnSaveCall_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedIndex = 1;
            btnAddNewCustomer_Click(sender, e);

        }

        private void btnNewOrderCall_Click(object sender, EventArgs e)
        {

        }



    }
}
